package com.ruoyi.project.product.product.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.product.sku.domain.ProductSku;
import com.ruoyi.project.product.sku.service.IProductSkuService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.product.product.domain.Product;
import com.ruoyi.project.product.product.service.IProductService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 商品管理Controller
 * 
 * @author wyh
 * @date 2024-08-05
 */
@RestController
@RequestMapping("/product/product")
public class ProductController extends BaseController
{
    @Autowired
    private IProductService productService;

    @Autowired
    private IProductSkuService productSkuService;

    /**
     * 查询商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('product:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(Product product)
    {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('product:product:export')")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Product product)
    {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productService.selectProductById(id));
    }

    /**
     * 新增商品管理
     */
    @PreAuthorize("@ss.hasPermi('product:product:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product)
    {
        product.setShelfStatus("否");
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改商品管理
     */
    @PreAuthorize("@ss.hasPermi('product:product:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Product product)
    {
        //校验是否上架
        String shelfStatus = product.getShelfStatus();
        if(shelfStatus.equals("是")){//上架
            //校验是否补全sku
            Long id = product.getId();
            ProductSku scProductSku = new ProductSku();
            scProductSku.setProductId(id);
            scProductSku.setIsReveal("是");
            List<ProductSku> scProductSkus = productSkuService.selectProductSkuList(scProductSku);
            if(ObjectUtils.isEmpty(scProductSkus)){
                return AjaxResult.error("请补全sku");
            }
        }

        //和原属性模板不一致
        Product scProduct1 = productService.selectProductById(product.getId());
        System.out.println("原模板id："+product.getAttrId()+";新模板id："+scProduct1.getAttrId());
        if(StringUtils.isNotBlank(product.getAttrId()) && !scProduct1.getAttrId().equals(product.getAttrId())){
            System.out.println("不相等");
            product.setShelfStatus("否");
            //清除原sku
            productSkuService.deleteSkuByProductId(product.getId());
        }

        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除商品管理
     */
    @PreAuthorize("@ss.hasPermi('product:product:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productService.deleteProductByIds(ids));
    }
}
